home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / PWarp / Warp.p < prev   
Text File  |  1995-03-29  |  5KB  |  214 lines

  1. program Pwarp;
  2.  
  3. {Based on Warp by Tony Mattis}
  4.  
  5. {Changes:}
  6. {• different colors on the stars}
  7. {• scaled sizes}
  8. {• works even without CQD}
  9.  
  10. {$IFC UNDEFINED THINK_PASCAL}
  11.         uses Types, QuickDraw, Events, Windows, Dialogs, Fonts, DiskInit,{} TextEdit, Traps, Desk, Memory,{}
  12.         SegLoad, Scrap, ToolUtils, OSEvents, OSUtils, Menus, Resources, Packages; {}
  13. {$ENDC}
  14.  
  15.     const
  16.         kNumOfStars = 30;    {was 70}
  17.         kProjDistance = 150; {was 450}
  18.         kLargeStar = 0;
  19.         kSmallStar = 1;
  20.         kVelocity = 6;
  21.  
  22.     type
  23.         Star = record
  24.                 x, y, z: Longint;            {3D location}
  25.                 size: Integer;                {How big?}
  26.                 starColor: RGBColor;    {Draw in this color}
  27.                 location: Point;            {Screen location}
  28.             end;
  29.  
  30.     var
  31.         gStarField: array[0..kNumOfStars] of Star;
  32.         gOrigin: Point;
  33.         gWindow: WindowPtr;
  34.         gColorFlag: Boolean;
  35. gScreenRect:Rect;
  36.     procedure InitToolbox;
  37.         var
  38.             theWorld: SysEnvRec;
  39.     begin
  40. {$IFC UNDEFINED THINK_PASCAL}
  41.         InitGraf(@qd.thePort);
  42.         InitFonts;
  43.         InitWindows;
  44.         InitMenus;
  45.         TEInit;
  46.         InitDialogs(nil);
  47.         qd.randSeed := TickCount;
  48.         gScreenRect := qd.screenBits.bounds;
  49. {$ELSEC}
  50.         randSeed := TickCount;
  51.         gScreenRect := screenBits.bounds;
  52. {$ENDC}
  53.         InitCursor;
  54.  
  55.         if noErr = SysEnvirons(1, theWorld) then
  56.             gColorFlag := theWorld.hasColorQD;
  57.  
  58.         if gColorFlag then
  59.             gWindow := NewCWindow(nil, gScreenRect, '', true, plainDBox, WindowPtr(-1), false, 0)
  60.         else
  61.             gWindow := NewWindow(nil, gScreenRect, '', true, plainDBox, WindowPtr(-1), false, 0);
  62.  
  63. {Make the window cover the entire screen}
  64.         RectRgn(gWindow^.visRgn, gScreenRect);
  65.  
  66.         SetPort(gWindow);
  67.         PaintRect(gWindow^.portRect);
  68.     end;
  69.  
  70.     function GetRandom (min: Integer; max: Integer): Integer;
  71.     begin
  72.         GetRandom := abs(Random) mod (max - min + 1) + min;
  73.     end; {GetRandom}
  74.  
  75.     procedure CreateStar (var aStar: Star);
  76.     begin
  77.         aStar.x := GetRandom(0, gOrigin.h) - gOrigin.h div 2;
  78.         aStar.y := GetRandom(0, gOrigin.v) - gOrigin.v div 2;
  79.         aStar.z := GetRandom(0, 150) + 125;
  80.  
  81.         aStar.size := GetRandom(0, 1);
  82.  
  83.         if gColorFlag then
  84.             begin
  85.                 aStar.starColor.red := Random;
  86.                 aStar.starColor.green := Random;
  87.                 aStar.starColor.blue := Random;
  88.  
  89. {Set one component to max so all stars are bright}
  90.                 case GetRandom(1, 3) of
  91.                     1: 
  92.                         aStar.starColor.red := -1;
  93.                     2: 
  94.                         aStar.starColor.green := -1;
  95.                     3: 
  96.                         aStar.starColor.blue := -1;
  97.                 end; {case}
  98.             end;
  99.  
  100.     end; {CreateStar}
  101.  
  102.     procedure WarpColor (starColor: RGBColor);
  103.     begin
  104.         if gColorFlag then
  105.             RGBForeColor(starColor)
  106.         else
  107.             ForeColor(whiteColor);
  108.     end; {WarpColor}
  109.  
  110.     procedure InitStarField;
  111.         var
  112.             loop: Integer;
  113.     begin
  114.         gOrigin.h := (gScreenRect.right - gScreenRect.left) div 2;
  115.         gOrigin.v := (gScreenRect.bottom - gScreenRect.top) div 2;
  116.  
  117.         for loop := 0 to kNumOfStars - 1 do
  118.             CreateStar(gStarField[loop]);
  119.     end; {InitStarField}
  120.  
  121.     procedure DrawLargeStar (aStar: Star);
  122.         var
  123.             starRect: Rect;
  124.             starSize: Integer;
  125.         const
  126.             kStarScale = 300;
  127.             kViewBase = 5;
  128.     begin
  129.         starSize := 1 + kStarScale div (aStar.z + kViewBase);
  130.         starRect.left := aStar.location.h;
  131.         starRect.right := starRect.left + starSize;
  132.         starRect.top := aStar.location.v;
  133.         starRect.bottom := starRect.top + starSize;
  134.  
  135.         PaintOval(starRect);
  136.     end; {DrawLargeStar}
  137.  
  138.     procedure DrawSmallStar (aPt: Point);
  139.     begin
  140.         MoveTo(aPt.h, aPt.v);
  141.         LineTo(aPt.h, aPt.v);
  142.     end;
  143.  
  144. {Make a projection from 3D space to the screen}
  145.     function Project (aStar: Star): Point;
  146.         var
  147.             starRect: Point;
  148.     begin
  149.         starRect.h := aStar.x * kProjDistance div aStar.z + gOrigin.h;
  150.         starRect.v := aStar.y * kProjDistance div aStar.z + gOrigin.v;
  151.  
  152.         Project := starRect;
  153.     end; {Project}
  154.  
  155. {Move a star, reset it if necessary}
  156.     procedure AnimateStar (var aStar: Star);
  157.     begin
  158.         aStar.z := aStar.z - kVelocity;
  159.         if aStar.z <= 0 then
  160.             CreateStar(aStar);
  161.  
  162.         aStar.location := Project(aStar);
  163.  
  164.         if aStar.location.h < 0 then
  165.             CreateStar(aStar)
  166.         else if aStar.location.h > gScreenRect.right then
  167.             CreateStar(aStar)
  168.         else if aStar.location.v > gScreenRect.bottom then
  169.             CreateStar(aStar)
  170.         else if aStar.location.v < 0 then
  171.             CreateStar(aStar);
  172.     end; {AnimateStar}
  173.  
  174.     procedure AnimateStarField;
  175.         var
  176.             loop: Integer;
  177.     begin
  178.         for loop := 0 to kNumOfStars - 1 do
  179.             begin
  180.                 ForeColor(blackColor);
  181.                 if gStarField[loop].size = kLargeStar then
  182.                     DrawLargeStar(gStarField[loop])
  183.                 else
  184.                     DrawSmallStar(gStarField[loop].location);
  185.  
  186.                 AnimateStar(gStarField[loop]);
  187.                 WarpColor(gStarField[loop].starColor);
  188.  
  189.                 if gStarField[loop].size = kLargeStar then
  190.                     DrawLargeStar(gStarField[loop])
  191.                 else
  192.                     DrawSmallStar(gStarField[loop].location);
  193.             end;
  194.     end; {AnimateStarField}
  195.  
  196.     var
  197.         startTime: Longint;
  198.  
  199. begin {main program}
  200.     InitToolbox;
  201.     InitStarField;
  202.     HideCursor;
  203.  
  204.     while not Button do
  205.         begin
  206.             startTime := TickCount;
  207.             AnimateStarField;
  208.             while TickCount < startTime + 1 do
  209.                 ;
  210.         end;
  211.  
  212.     ShowCursor;
  213.  
  214. end. {main program}